package swa2012.telco.client;

import swa2012.telco.broker.beans.*;
import swa2012.telco.provider.ClientProvider;
import swa2012.telco.provider.TelcoProvider;

import javax.swing.Timer;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;


import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Vector;

/**
 * @author lukas draschkowitz
 * @date 10.06.2012
 */

public class TelcoClient {
	private String id;
	private ClientProvider provider;
	private Collection<Message> messages;
	private Client clientObject;
	private boolean newMessages;
	public Timer timer;

	public TelcoClient(String id) throws URISyntaxException {
		this.newMessages = false;
		this.id = id;

		//TelcoProvider telcoProvider = new TelcoProvider("group12");
		//this.provider = telcoProvider.getClientProvider();

		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(ClientProvider.class);
		factory.setAddress("http://student.swa.univie.ac.at:8012/SWA/Provider");
		factory.create();
		this.provider = ((ClientProvider) factory.create());

		this.clientObject = this.provider.register(this.id);

//
//		int delay = 2000; // milliseconds
//		ActionListener taskPerformer = new ActionListener() {
//			public void actionPerformed(ActionEvent evt) {
//				// ...Perform a task...
//				// messages = this.provider.getMessages(this.getID());
//				Collection<Message> messages1 = getProvider().getMessages(getID());
//				if (!messages1.equals(messages)) {
//					newMessages = true;
//					messages = messages1;
//				}
//			}
//		};
//		// taskPerformer.
//		this.timer = new Timer(delay, taskPerformer);
//		timer.start();
	}

	public boolean newMessage() {
		return this.newMessages;
	}

	public Collection<Message> getMessages() {
		this.messages = this.provider.getMessages(this.id);
		return this.messages;
	}

	public Vector<LinkedHashMap<String,String>> getMessagesString() {
		Collection <Message> tmp = this.getMessages();
		Vector <LinkedHashMap<String,String>> returnresult = new Vector<LinkedHashMap<String,String>>();
		if(!tmp.isEmpty()){
			for(Message item: tmp){
				LinkedHashMap <String,String> result = new LinkedHashMap<String,String>();
				result.put("#FROM", item.getSender().getID());
				result.put("#TO", item.getReceiver().getID());
				result.put("#Message", "\n"+item.getText()+"\n");
				result.put("#TIME", ""+item.getTimestamp().toString());
				returnresult.add(result);
			}

		}else{
			LinkedHashMap <String,String> result = new LinkedHashMap<String,String>();
			result.put("#Message", "\n Sie haben keine Nachrichten! \n");
			returnresult.add(result);
		}

		return returnresult;

	}

	public String getID() {
		return id;
	}

	public void setID(String id) {
		this.id = id;
	}

	public ClientProvider getProvider() {
		return this.provider;
	}

	public void setProvider(ClientProvider provider) {
		this.provider = provider;
	}

	@Override
	public String toString() {
		return this.getClass().getSimpleName() + "[" + this.id + "@"
				+ this.provider + "]";
	}

	public void disconnect() {
		provider.deregister(this.id);
		timer.stop();
	}

	public Collection<Client> listClients() {
		Collection<Client> clients = this.provider.listClients();
		if (clients != null)
			return clients;
		return Collections.emptyList();
	}

	public LinkedHashMap<String,String> listClientsString(){
		Collection <Client> tmp = this.listClients();
		LinkedHashMap<String,String> returnresult = new LinkedHashMap<String,String>();
		String message = "";

		for(Client item: tmp){
			message = message + item.getID() + "\n";
		}

		returnresult.put("#Message", message);

		return returnresult;
	}

	/*
	 * public Collection<Message> listClients() { return
	 * provider.listClients(this.id);
	 *
	 *
	 * }
	 */
	public void sendMessage(Message msg) {
		this.provider.sendMessage(msg);
	}

	public void sendMessage(String receiver, String text) {
		Collection<Client> clients =  this.listClients();
		boolean sended = false;

		for(Client citem: clients){
			if(citem.getID().equals(receiver)){
				Message msg = new Message();
				msg.setReceiver(citem);
				msg.setSender(this.clientObject);
				msg.setText(text);
				msg.setTimestamp(System.nanoTime());
				this.provider.sendMessage(msg);
				sended = true;
				break;
			}
		}

		if(sended == false){
			Message msg = new Message();
			msg.setReceiver(this.clientObject);
			msg.setSender(this.clientObject);
			msg.setText("Konnte nicht übertragen werden, da " + receiver + "kein verbundener Client ist.");
			this.provider.sendMessage(msg);
		}
	}

}